package com.example.administrator.szb.activity.myyewu;

import android.app.Activity;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.bean.YWGLBean;
import com.example.administrator.szb.util.QTLog;
import com.example.zhouwei.library.CustomPopWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/2/27.
 */

public class YWPopWindow {
    private Activity context;
    private List<Integer> integerList = new ArrayList<>();
    public YWPopWindow(Activity context) {
        this.context = context;
    }

    public CustomPopWindow popWindow;//弹出框
    /**
     * 初始化popupwindow弹出框
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void showPopupwindow(int view_layout_id, int rootview_layout_id, final int position) {
        View popwindow_view = LayoutInflater.from(context).inflate(view_layout_id, null);


//        /**
//         * 如果是单选业务类型就将下面的清除确定布局移除
//         */
//        if (businessBean_choose.getData().get(0).getType() == 2){
//            popwindow_view.findViewById(R.id.view_popuwindow_ll_ywxq).setVisibility(View.GONE);
//        }
        View rootview = LayoutInflater.from(context).inflate(rootview_layout_id, null);
        //处理popWindow 显示内容
        handleListView(popwindow_view,position);
        if (view_layout_id != R.layout.view_progressbar) {
            popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                    .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            int id = -1;
                            for (int i=0;i<dataBean.getNext().get(position).getNext().size();i++){
                                if (dataBean.getNext().get(position).getNext().get(i).getGenre()==1){
                                    id=dataBean.getNext().get(position).getId();
                                }
                            }
                            QTLog.e(id);
                            if (id!=-1){
                                if (!integerList.contains((Integer)id)){
                                    integerList.add(id);
                                    dataBean.getNext().get(position).setGenre(1);
                                }
                            }else {
                                id=dataBean.getNext().get(position).getId();
                                if (integerList.contains((Integer)id)){
                                    integerList.remove((Integer)id);
                                    dataBean.getNext().get(position).setGenre(0);
                                }
                            }
                            if (onChangeListener!=null){
                                onChangeListener.onChange();
                            }
                        }
                    })
                    .setView(popwindow_view)//显示的布局，还可以通过设置一个View
                    .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                    .setBgDarkAlpha(0.7f) // 控制亮度
                    .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) //设置显示的大小，不设置就默认包裹内容
                    .setFocusable(true)//是否获取焦点，默认为ture
                    .setOutsideTouchable(true)//是否PopupWindow 以外触摸dissmiss
                    .create()//创建PopupWindow
                    .showAtLocation(rootview, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
        //显示PopupWindow

    }

    BaseAdapter<YWGLBean.NextBeanX.NextBean> adapter_next;//弹窗adapter
    public void handleListView(View view, final int position_data){
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.view_popuwindow_recycler_ywxq);
        initAdapter(position_data);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter_next);


        Button cancelAll = view.findViewById(R.id.view_popuwindow_button_cancel);
        Button sure = view.findViewById(R.id.view_popuwindow_button_sure);

        cancelAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清空
                for (int i=0;i<dataBean.getNext().get(position_data).getNext().size();i++){
                    dataBean.getNext().get(position_data).getNext().get(i).setGenre(0);
                }
                adapter_next.notifyDataSetChanged();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dissmiss();
            }
        });
    }
    YWGLBean dataBean;

    public void setDataBean(YWGLBean dataBean) {
        this.dataBean = dataBean;
    }

    private void initAdapter(final int position_data) {
        adapter_next = new BaseAdapter<YWGLBean.NextBeanX.NextBean>(context,dataBean.getNext().get(position_data).getNext()) {
            @Override
            public void convert(final int position, BaseViewHolder holder, final YWGLBean.NextBeanX.NextBean nextBean) {
                if (nextBean.getGenre() == 1){
                    ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setChecked(true);
                }else{
                    ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setChecked(false);
                }

                ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setEnabled(false);
                ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setClickable(false);

                ((TextView)holder.getView(R.id.ywxq_no_popuwindow_text_item)).setText(nextBean.getTitle());

            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.view_ywxq_popuwindow_item;
            }
        };


        adapter_next.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (dataBean.getNext().get(position_data).getType()==2){
                    //单选业务
                    for (int i=0;i<dataBean.getNext().get(position_data).getNext().size();i++){
                        dataBean.getNext().get(position_data).getNext().get(i).setGenre(0);
                    }
                    dataBean.getNext().get(position_data).getNext().get(position).setGenre(1);
                }else {
                    //多选业务
                    if (dataBean.getNext().get(position_data).getNext().get(position).getGenre()==1){
                        dataBean.getNext().get(position_data).getNext().get(position).setGenre(0);
                    }else {

                        for (int i=0;i<dataBean.getNext().get(position_data).getNext().size();i++){
                            if (dataBean.getNext().get(position_data).getNext().get(i).getIs_repeal()==1){
                                dataBean.getNext().get(position_data).getNext().get(i).setGenre(0);
                            }
                        }

                        if (dataBean.getNext().get(position_data).getNext().get(position).getIs_repeal()==1){
                            for (int i=0;i<dataBean.getNext().get(position_data).getNext().size();i++){
                                dataBean.getNext().get(position_data).getNext().get(i).setGenre(0);
                            }
                        }else if (dataBean.getNext().get(position_data).getNext().get(position).getIs_repeal()==3){
                            for (int i=0;i<dataBean.getNext().get(position_data).getNext().size();i++){
                                dataBean.getNext().get(position_data).getNext().get(i).setGenre(1);
                            }
                        }
                        dataBean.getNext().get(position_data).getNext().get(position).setGenre(1);


                    }
                }
                adapter_next.notifyDataSetChanged();
            }
        });
    }

    public List<Integer> getIntegerList() {
        return integerList;
    }

    public void setIntegerList(List<Integer> l) {

        integerList.clear();
        integerList.addAll(l);
    }
    OnChangeListener onChangeListener;

    public void setOnChangeListener(OnChangeListener onChangeListener) {
        this.onChangeListener = onChangeListener;
    }

    interface OnChangeListener{
        void onChange();
    }
}
